home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.06 Oct 92 / One-Application Patches / wrap events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-08  |  2.1 KB  |  84 lines  |  [TEXT/KAHL]

  1. /*    ---------------------------------------------
  2.         wrap events.c    Watch keyboard events to do
  3.                                         word wrapping, and watch mouse
  4.                                         events to handle clicks in the
  5.                                         wrap icon.
  6.         
  7.         THINK C "Set Project Type..." settings:
  8.         code resource, type 'OAPe', ID 1000,
  9.         custom header, preloaded and locked,
  10.         file type 'rsrc', file creator 'RSED'.
  11.         ---------------------------------------------
  12. */
  13. #include <Script.h>
  14. #include "defs.h"
  15. void main( EventRecord *evt );
  16.  
  17. #define        RETURN_MESSAGE            0x0002240DL
  18. #define        WRAP_FACTOR                    10
  19. #define        SCROLLBAR_WIDTH            16
  20. #define        MODIFIER_KEYS                0x1F00
  21.  
  22. void main( EventRecord *evt )
  23. {
  24.     WindowPeek        front;
  25.     Wrap_info        **wrap_info;
  26.     Rect                    icon_rect;
  27.     RgnHandle        redraw_rgn;
  28.     GrafPtr            save_port;
  29.     short                wrap_margin, font_size;
  30.     
  31.     wrap_info = (Wrap_info **)
  32.                                     GetResource( 'OAP1', 128 );
  33.     if (wrap_info == NIL)
  34.         return;
  35.     front = (WindowPeek) FrontWindow();
  36.     
  37.     if ( (evt->what == keyDown) &&
  38.         ((evt->message & charCodeMask) == ' ') &&
  39.         ((evt->modifiers & MODIFIER_KEYS) == 0) &&
  40.         ((**wrap_info).wrap) )
  41.     {
  42.         font_size = front->port.txSize;
  43.         if (font_size == 0)
  44.             font_size = GetDefFontSize();
  45.         wrap_margin = font_size * WRAP_FACTOR
  46.                 + SCROLLBAR_WIDTH;
  47.         if ( (**wrap_info).last_insertion_point >
  48.                 front->port.portRect.right - wrap_margin )
  49.         {
  50.             (**wrap_info).last_insertion_point = 0;
  51.             evt->message = RETURN_MESSAGE;
  52.         }
  53.     } // end if keyDown && space
  54.  
  55.     else if (evt->what == mouseDown)
  56.     {
  57.         /*
  58.             If the click was in our little icon in the
  59.             window's title bar, then toggle the wrapping
  60.             state.
  61.         */
  62.         icon_rect = (**(front->strucRgn)).rgnBBox;
  63.         icon_rect.left += 22;
  64.         icon_rect.top += 6;
  65.         icon_rect.right = icon_rect.left + 8;
  66.         icon_rect.bottom = icon_rect.top + 8;
  67.         
  68.         if (PtInRect( evt->where, &icon_rect ))
  69.         {
  70.             evt->what = nullEvent;
  71.             (**wrap_info).wrap = !(**wrap_info).wrap;
  72.             ChangedResource( (Handle) wrap_info );
  73.             
  74.             redraw_rgn = NewRgn();
  75.             RectRgn( redraw_rgn, &icon_rect );
  76.             GetPort( &save_port );
  77.             PaintOne( front, redraw_rgn );
  78.             SetPort( save_port );
  79.             DisposeRgn( redraw_rgn );
  80.         }
  81.     } // end if mouseDown
  82.         
  83. }
  84.